package com.server.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.ICommonDataService;
import com.server.service.ISurveyTestService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

/**
 * @author wufeng
 * @date 创建时间：2016年8月29日 下午4:23:34 
 */
@Service("SurveyService")
public class SurveyTestService implements ISurveyTestService{

	private static String tableName_res = "education.survey_res";
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public boolean add(Map<String, Object> inputMap, String operator) {
		boolean isSuccess = commonDataService.add(operator, tableName_res, inputMap);
		return isSuccess;
	}

	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput) {
		String sortStr = " createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName_res, null, sortStr, requestInput);
		return listData;
	}

	@Override
	public boolean update(Map<String, Object> inputMap, String operator,
			String id) {
		boolean isSuccess = commonDataService.update(operator, tableName_res, "id", id, inputMap);
		return isSuccess;
	}

	@Override
	public boolean delete(String id) {
		String sql = "delete from "+tableName_res+" where id = '"+id+"'";
		boolean isSuccess = true;
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}

	@Override
	public Map<String, Object> getMap(String id) {
		Map<String, Object> map = commonDataService.getFormData(tableName_res, "id", id, null, null);
		return map;
	}

	@Override
	public boolean isRight(String id, String operator) {
		String sql = "select creater from "+tableName_res+" where id = '"+id+"'";
		String authorship = jdbcTemplate.queryForObject(sql, String.class);
		if(operator.equals(authorship)){
			return true;
		}
		return false;
	}

	@Override
	public boolean isUse(String id) {
		boolean isUse = false;
		String sql = "select useCount from education.survey_res where id = '"+id+"'";
		try {
			int count = jdbcTemplate.queryForObject(sql, Integer.class);
			if(count >= 1){
				isUse = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isUse;
	}

}
